﻿//content load is complete.
$(window).load(function() {
    _pageDataDownloadIsCompleted = true;

    //loading alert
    $("#divLoading").css("display", "none");
});

//html load is complete.
$(document).ready(function() {
    //loupe
    $('.magnify').loupe();

    //dialog
    SetDialogs();

    //Tabs
    SetTabs();
});

//***<Tabs
function SetTabs() {
    var ids = new Array('home');
    var id;

    for (var i = 0; i < ids.length; i++) {
        id = '#tabs-' + ids[i];

        //fouc
        $(id).removeClass('NoDisplay');
        $(id).tabs();
    }
}
//***Tabs>

//***<dialog
function SetDialogs() {
    var ids = new Array('changepassword', 'maintools');
    var id;
    var dialog;
    var widthValue;

    for (var i = 0; i < ids.length; i++) {
        id = '#dialog-' + ids[i];

        widthValue = 300;
        switch (id) {
            case '#dialog-changepassword':
                widthValue = 400;
                break;
        }

        dialog = $(id).dialog({
            modal: true,
            autoOpen: false,
            resizable: true,
            width: widthValue
        });

        dialog.parent().appendTo(jQuery("form:first"));
    }
}

function ShowDialog(dialogName) {
    var dialogId = "#dialog-" + dialogName;
    $(dialogId).dialog('open');
}
//***dialog>

//***<calendar
function SetDatePicker(id, regional) {
    id = '#' + id;
    switch (regional) {
        case 'gregorian':
            $(id).datepicker({ regional: '' });
            break;
        case 'jalali':
            $(id).datepicker();
            break;
        default:
    }
}
//***calendar>

$(document).ready(function () {

    jQuery.fn.center = function () {
        this.css("position", "fixed");
        this.css("top", (($(window).height() - this.outerHeight()) / 2) +
            $(window).scrollTop() + "px");
        this.css("left", (($(window).width() - this.outerWidth()) / 2) +
            $(window).scrollLeft() + "px");
        return this;
    };

    jQuery.fn.centerRight = function () {
        this.css("position", "fixed");
        this.css("top", (($(window).height() - this.outerHeight()) / 2) +
            $(window).scrollTop() + "px");
        this.css("right", "0px");
        return this;
    };

});

//***<notify
//فقط صفحه اصلی این مقدار را مثبت می کند
var showNofityWithDelay = false;

$(document).ready(function () {
    var millis = 0;
    var code = 'nofityAction()';

    if (showNofityWithDelay) {
        millis = 5000;
    }

    setTimeout(code, millis);
});

function nofityAction() {
    $('.notification.ondemand').notify({ type: 'ondemand' });

    if (showNofityWithDelay) {
        $('#aArrow').click();

        $('#imgClose').click(function () {
            $('#divArrow').removeClass('hide');
        });
    }
    else {
        $('#divArrow').removeClass('hide');
    }
}
//***>

//***<number
//اعداد دو دسته هستند:
//1. غیر اعشاری
//2. اعشاری:
//	دارای . هستند
//هر دو نوع در این بخش پشتیبانی شده اند.

//e.g. onkeyup="javascript: RemoveNotNumber(this);"
function RemoveNotNumber(ele) {
    var value = $(ele).val().toString();
    //اگر کاربر بیشتر از یک 
    //.
    //وارد کند مقدار بعد از
    //.
    //دوم حذف می شود
    var split = value.split('.');
    var numeric = split[0];
    var decimal = split.length > 1 ? split[1] : '';
    var reg = /\D/g;
    var result;

    if (reg.test(numeric))
        numeric = numeric.replace(reg, '');

    if (reg.test(decimal))
        decimal = decimal.replace(reg, '');

    result = numeric + (split.length > 1 ? '.' + decimal : '');

    $(ele).val(result);
}

//onkeyup = "javascript: SeperateDigits(this);"
function SeperateDigits(ele) {
    RemoveNotNumber(ele);

    var value = $(ele).val();
    var comma = ',';
    var period = '.';
    var split = value.split('.');
    var numeric = split[0];
    var decimal = split.length > 1 ? period + split[1] : '';
    var reg = /(\d+)(\d{3})/;
    var result;

    while (reg.test(numeric)) {
        numeric = numeric.replace(reg, '$1' + comma + '$2');
    }

    result = numeric + decimal;

    $(ele).val(result);
}
//***>

function workConfig() {
    var counter = 1;
    var workFront = "#divWorkFront";
    var workBack = "#divWorkBack";
    var workFrontId = workFront + counter;
    var workBackId = workBack + counter;

    //add fadein and fadeout
    while ($(workFrontId).length > 0) {

        $(workFrontId).mouseenter(function () {
            var id = this.id.toString();
            //عدد آخر شناسه مورد نظر است
            var endNum = id.substring(workFront.length - 1);

            $(workBack + endNum).fadeIn(2000);
            $(workFront + endNum).fadeOut(2000);
        });

        $(workBackId).mouseleave(function () {
            var id = this.id.toString();
            var endNum = id.substring(workBack.length - 1);

            $(workFront + endNum).fadeIn(2000);
            $(workBack + endNum).fadeOut(2000);
        });

        counter += 1;
        workFrontId = workFront + counter;
        workBackId = workBack + counter;
    }

    var groupsCount = parseInt($("#hdnGroupsCount").val());
    var currentGroup = 1;

    //navigate next
    var navigateNextWork = function () {
        if (currentGroup < groupsCount) {
            $("#divWorksContainer").animate(
                {
                    left: '-=850'
                },
                1000);

            currentGroup += 1;
            $("#aPrv").css('background-image', 'url(/Images/arrow-paging.png)');
            if (currentGroup == groupsCount)
                $("#aNxt").css('background-image', 'url(/Images/arrow-paging-disabled.png)');
        }
    };

    //navigate previous
    var navigatePreviousWork = function () {
        if (currentGroup > 1) {
            $("#divWorksContainer").animate(
                {
                    left: '+=850'
                },
                1000);

            currentGroup -= 1;
            $("#aNxt").css('background-image', 'url(/Images/arrow-paging.png)');
            if (currentGroup == 1)
                $("#aPrv").css('background-image', 'url(/Images/arrow-paging-disabled.png)');
        }
    };

    $("#aNxt").click(navigateNextWork);
    $("#aPrv").click(navigatePreviousWork);
};

//***>

function ShowConfirm(msg) {
    return confirm(msg);
}

function ExecuteWithDelay(command, time) {
    setTimeout(command, time);
}

//***< MyAnimation

function ShowFace(faceNo) {
    switch (faceNo) {
        case 'Face01':
            {
                $("#divFace01").animate({ width: "200px", height: "200px", left: "10px" }, 'slow', function () {
                    ExecuteWithDelay("ShowFace('Face02')", 2000);
                });

                break;
            }
        case 'Face02':
            {
                //hide Face 01
                $("#divFace01").animate({ width: "0px", height: "0px", left: "110px" }, 'slow', function () {
                    //show Face 02
                    $("#divFace02").css('display', 'block');
                    $("#divFace02").animate({ width: "200px", height: "200px", left: "10px" }, 'slow', function () {
                        ExecuteWithDelay("ShowThoughtBubble('Thought01')", 2000);
                    });
                });

                break;
            }
        case 'Face03':
            {
                $("#divFace02").animate({ width: "0px", height: "0px", left: "110px" }, 'slow', function () {
                    $("#divFace03").css('display', 'block');
                    $("#divFace03").animate({ width: "200px", height: "200px", left: "10px" }, 'slow', function () {
                        ExecuteWithDelay("ShowFace('Face04')", 2000);
                    });
                });

                break;
            }
        case 'Face04':
            {
                $("#divFace03").animate({ width: "0px", height: "0px", left: "110px" }, 'slow', function () {
                    $("#divFace04").css('display', 'block');
                    $("#divFace04").animate({ width: "200px", height: "200px", left: "10px" }, 'slow', function () {
                        ExecuteWithDelay("ShowThoughtBubble('Thought02')", 2000);
                    });
                });

                break;
            }
    }
}

function ShowThoughtBubble(thoughtNo) {
    switch (thoughtNo) {
        case "Thought01":
            {
                $("#spanThoughtBubble").text(animationSentenses[0]);
                break;
            }
        case "Thought02":
            {
                $("#spanThoughtBubble").text(animationSentenses[1]);
                break;
            }
    }

    $("#divThoughtBubble").fadeIn('slow', function () {
        ExecuteWithDelay("MoveThoughtBubble('" + thoughtNo + "')", 0);
    });
}

function HideThoughtBubble(thoughtNo) {
    switch (thoughtNo) {
        case "Thought01":
            {
                $("#divThoughtBubble").fadeOut('slow', function () {
                    ExecuteWithDelay("ShowFace('Face03')", 2000);
                });

                break;
            }
        case "Thought02":
            {
                $("#divThoughtBubble").fadeOut('slow', function () {
                    $("#divFace04").animate({ width: "0px", height: "0px", left: "110px" }, 'slow', function () {
                    });
                });

                break;
            }
    }
}

function MoveThoughtBubble(thoughtNo) {
    $("#divThoughtBubble").animate({ "left": "-=10px" }, 500, function () {
        $("#divThoughtBubble").animate({ "left": "+=20px" }, 500, function () {
            $("#divThoughtBubble").animate({ "left": "-=10px" }, 500, function () {
                ExecuteWithDelay("HideThoughtBubble('" + thoughtNo + "')", 0);
            });
        });
    });
}

//***> MyAnimation

//*** Tiles <

var _tileMouseEnterActionCompleted = [true, true, true];
var _tileMouseLeaveActionCompleted = [true, true, true];

$(document).ready(function () {

    $("#divAllTiles").mouseenter(function () {
        AllTilesMouseEnter();
    });

    $("#divTile01").mouseenter(function () {
        TileMouseEnter("Tile01");
    });
    $("#divTile02").mouseenter(function () {
        TileMouseEnter("Tile02");
    });
    $("#divTile03").mouseenter(function () {
        TileMouseEnter("Tile03");
    });

    $("#divTile01").mouseleave(function () {
        TileMouseLeave("Tile01", false);
    });
    $("#divTile02").mouseleave(function () {
        TileMouseLeave("Tile02", false);
    });
    $("#divTile03").mouseleave(function () {
        TileMouseLeave("Tile03", false);
    });

    $("#divAllTiles").mouseleave(function () {
        AllTilesMouseLeave();
    });

});

function AllTilesMouseEnter() {
    $("#divAllTiles").css("opacity", 1);
}

function TileMouseEnter(tileName) {
    var arrayIndex;
    var marginTopValue;

    switch (tileName) {
        case "Tile01":
            {
                arrayIndex = 0;
                marginTopValue = "10px";

                break;
            }
        case "Tile02":
            {
                arrayIndex = 1;
                marginTopValue = "0px";

                break;
            }
        case "Tile03":
            {
                arrayIndex = 2;
                marginTopValue = "10px";

                break;
            }
    }

    TileFocus(tileName);

    if (_tileMouseEnterActionCompleted[arrayIndex]) {
        _tileMouseEnterActionCompleted[arrayIndex] = false;

        $("#div" + tileName).animate({ marginTop: marginTopValue }, 200, function () {
            _tileMouseEnterActionCompleted[arrayIndex] = true;
        });
    }

}

function AllTilesMouseLeave() {
    $("#divAllTiles").css("opacity", 0.8);
    //reset tiles
    TileFocus("Tile02");
    TileMouseLeave("Tile01", true);
    TileMouseLeave("Tile02", true);
    TileMouseLeave("Tile03", true);
}

function TileMouseLeave(tileName, forceAnimation) {
    var arrayIndex;
    var marginTopValue;

    switch (tileName) {
        case "Tile01":
            {
                arrayIndex = 0;
                marginTopValue = "20px";

                break;
            }
        case "Tile02":
            {
                arrayIndex = 1;
                marginTopValue = "10px";

                break;
            }
        case "Tile03":
            {
                arrayIndex = 2;
                marginTopValue = "20px";

                break;
            }
    }

    if (_tileMouseLeaveActionCompleted[arrayIndex] || forceAnimation) {
        _tileMouseLeaveActionCompleted[arrayIndex] = false;

        $("#div" + tileName).animate({ marginTop: marginTopValue }, 200, function () {
            _tileMouseLeaveActionCompleted[arrayIndex] = true;
        });
    }

}

function TileFocus(tileName) {
    switch (tileName) {
        case "Tile01":
            {
                $("#divTile01").css("z-index", 30);
                $("#divTile02").css("z-index", 20);
                $("#divTile03").css("z-index", 10);

                $("#divTile01Content").css("box-shadow", "0px 0px 20px #1f1f1f");
                $("#divTile02Content").css("box-shadow", "0px 0px 10px #3d3d3d");
                $("#divTile03Content").css("box-shadow", "0px 0px 10px #3d3d3d");

                break;
            }
        case "Tile02":
            {
                $("#divTile01").css("z-index", 10);
                $("#divTile02").css("z-index", 30);
                $("#divTile03").css("z-index", 10);

                $("#divTile01Content").css("box-shadow", "0px 0px 10px #3d3d3d");
                $("#divTile02Content").css("box-shadow", "0px 0px 20px #1f1f1f");
                $("#divTile03Content").css("box-shadow", "0px 0px 10px #3d3d3d");

                break;
            }
        case "Tile03":
            {
                $("#divTile01").css("z-index", 10);
                $("#divTile02").css("z-index", 20);
                $("#divTile03").css("z-index", 30);

                $("#divTile01Content").css("box-shadow", "0px 0px 10px #3d3d3d");
                $("#divTile02Content").css("box-shadow", "0px 0px 10px #3d3d3d");
                $("#divTile03Content").css("box-shadow", "0px 0px 20px #1f1f1f");

                break;
            }
    }
}

//*** Tiles >